<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class FlowGroup extends Model 
{
    protected $table = 'workflow_group';
    
    protected $insert = ['isvalid' => 1];
    
    protected $autoWriteTimestamp = true;
    
    
    /**
     * 获取用户所属权限组
     * @param int $uid 用户id
     * @return  array
     */
    public function getUserAuthGroup($uid)
    {
        $groupUserModel = db('org_workflow_group_user');
        $authGroup = $map = array();
        $map['isvalid'] = 1;
        $map['uid'] = $uid;

        $groups = $groupUserModel->field('group_id')->where($map)->select();
        if ($groups) {
            foreach ($groups as $val) {
                array_push($authGroup, $val['group_id']);
            }
        } 
        
        return $authGroup;
    }

    /**
     * 根据caseId 获取流程下一步经办人
     * @param int $caseId 事项id
     * @param int $nodeId 节点
     * @return array
     */
    public function getNextStepHandler ($caseId, $nodeId)
    {

        if ($nodeId == 12) {//送抵押
            $sql = "call getnode12user(".$caseId.", 12)";
        } else {//3 送审 8 送批公积金
            $sql = "call getnodeuser(".$caseId.", ".$nodeId.")";
        }

        $sqlResult = Db::query($sql);
        if(isset($sqlResult[0]['type'])) {
            $result_arr = $sqlResult[0];
        }else if(isset($sqlResult[0][0]['type'])){
            $result_arr = $sqlResult[0][0];
        }else{
            $result_arr = array();
        }

        $confData = $result_arr;
        if (isset($result_arr['type']) && 1==$result_arr['type']) {
            $groupId = $result_arr['role_id'];
        } else {
            $confData['type'] = $result_arr['type'];
            $confData['role_id'] = $result_arr['role_id'];
        }

        //判断组的成员个数
        if (isset($groupId) && !empty($groupId)) {
            $groupUsers = db('org_workflow_group_user')->where(array('isvalid'=>1,'group_id'=>$groupId))->select();

            if (count($groupUsers) == 1) {
                $confData['type'] = 0;
                $confData['role_id'] = $groupUsers[0]['uid'];
            }
        }
        return $confData;

    }

}

 